<%@ page language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.tomcat.util.http.fileupload.*"%>
<html>
<head><title>Read from mySQL Database</title>
</head>
<body>
<center>

<%
	class XSSchecker
	{
		public String sanitize(String string) 
		{
			string.replaceAll("(?i)<script.*?>.*?</script.*?>", "");   	
			string.replaceAll("(?i)<.*?javascript:.*?>.*?</.*?>", ""); 
			string.replaceAll("(?i)<.*?\\s+on.*?>.*?</.*?>", "");
			return string;
		}
	};

       try
       {
		XSSchecker checker = new XSSchecker();

		if(session.getAttribute("logged_in") == "true")
		{

			out.println("<p><h2>Logged In</p></h2>");

			if(session.getAttribute("user_level") == "administrator")
			{

				if(request.getParameter("id") == null || checker.sanitize((String)request.getParameter("id")) == "") 
				{
					out.println("<p><h1>You must select a presentation !!</h1></p>");
					out.println("  <a href=menu.jsp>Main Menu</a> ");
				}
				else
				{

					String DRIVER = "com.mysql.jdbc.Driver";
					Class.forName(DRIVER);

					Connection con=null;
					ResultSet rst=null;
					ResultSet rstQuery=null;
					PreparedStatement stmt=null;
					PreparedStatement  stmtQuery=null;


					String url="jdbc:mysql://ecstiger.cs.andrews.edu/d562_2010_01?user=u562_2010_01&password=YPJ8f4We";
					con=DriverManager.getConnection(url);
					stmt = con.prepareStatement("SELECT * FROM presentation WHERE id = ?;");
					stmt.setString(1, checker.sanitize((String)request.getParameter("id")));
					rst=stmt.executeQuery();


					if(rst.next())
					{
				
						String FileName = rst.getString(3);
						int UserId = rst.getInt(4);

						//retrieve username from user table
						stmtQuery=con.prepareStatement("SELECT * FROM user WHERE id = '" + UserId + "';");
						rstQuery = stmtQuery.executeQuery();
 
						rstQuery.next();
						String UserName = rstQuery.getString(3);

						//delete file on server
						File f = new File("/var/lib/tomcat6/webapps/cptr562-2010-01/files/" + UserName + "/" + FileName);
						f.delete();
 
						//delete record in "screen_presentations" table
						stmt=con.prepareStatement("DELETE FROM `d562_2010_01`.`screen_presentations` WHERE presentation_id = ?;");
						stmt.setString(1, checker.sanitize((String)request.getParameter("id")));
						stmt.executeUpdate();

						//delete record in "presentation" table
						stmt=con.prepareStatement("DELETE FROM `d562_2010_01`.`presentation` WHERE id = ?;");
						stmt.setString(1, checker.sanitize((String)request.getParameter("id")));
						stmt.executeUpdate();
			
						out.println("<p><h1>The presentation is Deleted!! </h1></p>");
						out.println("<a href=menu.jsp>Main Menu</a> ");
						//response.setHeader("Refresh", "2; URL=./delete_presentation.jsp"); 

					}
					else
					{
						out.println("<p><h1>The presentation is NOT in the database !! </h1></p>");
						out.println("<a href=menu.jsp>Main Menu</a> ");
						//response.setHeader("Refresh", "2; URL=./delete_presentation.jsp"); 

					}

					rst.close();
					rstQuery.close();
					stmt.close();
					stmtQuery.close();
					con.close();
				}
			}
		}
		else
		{
			out.println("Not Logged In");
		}
	}

	catch(Exception e)
	{
		out.println(e);
	}

%>
</center>
</body>
</table>
</center>
</div>


</body>
</html>